package servlet;

import entity.Distribution;
import model.Picture;
import service.DistributionService;
import service.impl.DistributionServiceImpl;
import urity.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/DistributionServlet")
@MultipartConfig
public class DistributionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //定义session对象
        HttpSession session = request.getSession();
        //创建Writer对象
        PrintWriter out = response.getWriter();
        DistributionService service=new DistributionServiceImpl();

        String opr = request.getParameter("opr");
        if(opr.equals("list")){//查询
            int pageNo = Integer.parseInt(request.getParameter("pageNo"));
            //所展示的数据数
            int pageSize=3;
            Page page = service.selectLimit(pageNo, pageSize);
            request.setAttribute("page",page);
            //转发
            request.getRequestDispatcher("express_list.jsp").forward(request,response);

        }else if(opr.equals("update")){//修改
            //名称
            String name = request.getParameter("dName");
            //描述
            String dDescribe = request.getParameter("dDescribe");
            //id
            int id = Integer.parseInt(request.getParameter("id"));
            //图片二进制
            Part filePath = request.getPart("filePath");
            //路径
            String url="Picture/";
            //图片绝对路径
            String realPath = getServletContext().getRealPath(url);
            Picture picture=new Picture();
            //图片名称
            String s = picture.filesUploadModel(filePath, realPath);

            Distribution d=new Distribution();
            d.setdName(name);
            d.setdDescribe(dDescribe);
            d.setdPath(url+s);
            d.setId(id);

            int update = service.update(d);
            if(update>0){
                response.sendRedirect("express_list.jsp");
            }else {
                out.print("<script>alert(\"修改失败\");history.go(-1);</script>");
            }
        }else if (opr.equals("insert")){//新增
            //名称
            String name = request.getParameter("dName");
            //描述
            String dDescribe = request.getParameter("dDescribe");

            //图片二进制
            Part filePath = request.getPart("filePath");
            //路径
            String url="Picture/";
            //图片绝对路径
            String realPath = getServletContext().getRealPath(url);
            Picture picture=new Picture();
            //图片名称
            String s = picture.filesUploadModel(filePath, realPath);

            s=s.substring(0,s.length()-1);
            Distribution d=new Distribution();
            d.setdName(name);
            d.setdDescribe(dDescribe);
            d.setdPath(url+s);
            int insert = service.insert(d);
            if(insert>0){
                response.sendRedirect("express_list.jsp");
            }else {
                  out.print("<script>alert(\"新增失败\");history.go(-1);</script>");
            }
        }else if(opr.equals("selectOne")){
            int id = Integer.parseInt(request.getParameter("id"));
            Distribution distribution = service.selectOne(id);
            request.setAttribute("distribution",distribution);
            request.getRequestDispatcher("express_detail.jsp").forward(request,response);
        }else if(opr.equals("delete")){
            int id = Integer.parseInt(request.getParameter("id"));
            int delete = service.delete(id);
            if(delete>0){
                out.print(true);
            }else {
                out.print(false);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
